#ifndef GLWIDGET_H
#define GLWIDGET_H

#include<QGLWidget>

#include<QVector>
#include<QString>


#include "./drawables/drawable.h"
#include "./selectable/selectable.h"
#include "./drawables/coordinategrid.h"
#include "./drawables/coordstransformer.h"
#include "drawables/logcoordstransformer.h"
#include "drawables/emfgrid.h"

class GLWidget : public QGLWidget
{
    Q_OBJECT
protected:
    QVector<Drawable*> _drawables;
    CoordinateGrid _grid;
    QRectF _viewport;
    int _scale;
    GLfloat _translateX;
    GLfloat _translateY;
    QPointF _lastPos;
    int _selectedItem;

    int _scaleXMultiplier;
    int _scaleYMultiplier;

    LogCoordsTransformer _logCoordsTransformer;
    CoodrsTransformer *_currentCoordsTransformer;

    EmfGrid *_emfGrid;


protected:
    void _calcTotalBounding();
    virtual void _applyScaleAndTranslate();
    void _setGridStep();


    QPointF _projectWindowCoordinates(QPointF windowCoords);


    enum { MIN_SCALE=1, MAX_SCALE=40000, SCALE_STEP=2};
    enum { GRID_STEP = 100 };
    enum { INITIAL_SCALE_MULTIPLIER = 1024 };


public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();
    void addDrawable(Drawable* object);
    void autoScale();
    void clear();
    virtual void selectObject(int id)
    {
        id++;
    }

    void switchToLinearCoords();
    void switchToLogarithmicCoords();

    void increaseXScale();
    void decreaseXScale();
    void increaseYScale();
    void decreaseYScale();
    void oneToOne();

    void setEmfGrid(EmfGrid *grid);
    EmfGrid * getEmfGrid();
signals:
    void sendEmfInfo(QMap <int, QPointF> *infoMap);
    void selectedItem(int id);

protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);

};

#endif
